//
//  AttentionTableViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 15/12/30.
//  Copyright © 2015年 JC. All rights reserved.
//

#import "AttentionTableViewController.h"
#import "AttentionTableViewCell.h"
#import "NextAttentionViewController.h"
#import "model_attention.h"

#import "MJRefresh.h"
#import "DataProvider.h"
#import "SVProgressHUD.h"
#import "UIImageView+WebCache.h"
@interface AttentionTableViewController ()

//存储数据
@property (nonatomic, strong) NSMutableArray * arr_data;


@end

@implementation AttentionTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self example01];
    
    [self p_navigationController];
    //注册
    [self.tableView registerClass:[AttentionTableViewCell class] forCellReuseIdentifier:@"cell_attention"];
    //清除空余分割线
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self p_data];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"我的关注";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr_data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    model_attention * model = self.arr_data[indexPath.row];
    
    AttentionTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_attention" forIndexPath:indexPath];
    
    //加数据
    cell.image.layer.masksToBounds = YES;
    NSString *url = [NSString stringWithFormat:@"%@upload/%@",Url,model.headportain];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"头像"]];
    cell.name.text = model.name;
    cell.detail.text = model.sign;
    cell.date.text = model.intime;
    
    
    return cell;
}
//行高
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    model_attention * model = self.arr_data[indexPath.row];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NextAttentionViewController * nextAttentionViewController = [[NextAttentionViewController alloc] init];
    
    nextAttentionViewController.userid = model.userid;
    
    [self showViewController:nextAttentionViewController sender:nil];
}

#pragma mark - 数据
- (void)p_data
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [dataprovider setDelegateObject:self setBackFunctionName:@"Attention:"];
    
    [dataprovider AttentionWith:[defaults objectForKey:@"merchantid"] andpagenumber:@"1" andpagesize:@"50"];
}

- (void)Attention:(id )dict
{
    NSLog(@"%@",dict);
    self.arr_data = nil;
    
    if ([dict[@"status"][@"succeed"] intValue] == 1) {
        @try
        {
            for (NSDictionary * dict_data in dict[@"data"]) {
                
                model_attention * model = [[model_attention alloc] init];
                
                [model setValuesForKeysWithDictionary:dict_data];
                
                [self.arr_data addObject:model];
            }
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
            [self.tableView reloadData];
            
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)arr_data
{
    if(_arr_data == nil)
    {
        self.arr_data = [NSMutableArray array];
    }
    
    return _arr_data;
}

#pragma mark - 下拉刷新
- (void)example01
{
    __weak __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [weakSelf p_data];
        
        [weakSelf loadNewData];
        
    }];
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}

-(void)example02
{
    __weak __typeof(self) weakSelf = self;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadNewData];
        
    }];
    
}

- (void)loadNewData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    });
    
}

@end
